/*
 * @Author: your name
 * @Date: 2020-12-28 10:57:05
 * @LastEditTime: 2021-01-04 16:08:55
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \H5 2010 studyd:\H5 2010 练习\第三阶段 Vue + node\DAY 7\panda\src\router\index.js
 */
import Vue from "vue";
import VueRouter from "vue-router";
import store from "@/store";

// 单独把 routes 数组 提出去
import routes from "./routes";

Vue.use(VueRouter);

const router = new VueRouter({
  mode: "history",
  routes
});

// 权限路径数组
const permissionList = ["/mine", "/order"];

// 权限拦截处理
// 全局前置守卫
// to: 跳转到目标路由对象
router.beforeEach((to, from, next) => {
  console.log("全局前置守卫", to);
  if (permissionList.includes(to.path)) {
    // 如果在权限路径数组中存在目标路由对象的路径信息
    // 则说明需要用户登录才能访问
    // 获取用户在 store 中保存的登录 token 信息
    if (store.state.user.token) {
      // token 存在，则说明用户已经登陆过
      next();
    } else {
      next({
        name: "Login",
        params: {
          realRoute: to
        }
      });
    }
  } else {
    next();
  }
});

export default router;
